package com.suda.controller;

import com.github.pagehelper.PageInfo;
import com.suda.pojo.Customer;
import com.suda.service.CustomerService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:zhuyongjie
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/customer")
@CrossOrigin
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /***
     * Customer分页条件搜索实现
     * @param customer
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false)  Customer customer, @PathVariable  int page, @PathVariable  int size){
        //调用CustomerService实现分页条件查询Customer
        PageInfo<Customer> pageInfo = customerService.findPage(customer, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Customer分页搜索实现
     * @param page:当前页
     * @param limit:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search" )
    public Result<PageInfo> findPage(int page, int limit){
        //调用CustomerService实现分页查询Customer
        PageInfo<Customer> pageInfo = customerService.findPage(page, limit);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param customer
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Customer>> findList(@RequestBody(required = false)  Customer customer){
        //调用CustomerService实现条件查询Customer
        List<Customer> list = customerService.findList(customer);
        return new Result<List<Customer>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用CustomerService实现根据主键删除
        customerService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Customer数据
     * @param customer
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Customer customer,@PathVariable String id){
        //设置主键值
        System.out.println(customer.toString());
        customer.setCustomerId(id);
        //调用CustomerService实现修改Customer
        customerService.update(customer);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Customer数据
     * @param customer
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   Customer customer){
        //调用CustomerService实现添加Customer
        customerService.add(customer);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Customer数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Customer> findById(@PathVariable String id){
        //调用CustomerService实现根据主键查询Customer
        Customer customer = customerService.findById(id);
        return new Result<Customer>(true,StatusCode.OK,"查询成功",customer);
    }

    /***
     * 查询Customer全部数据
     * @return
     */
    @GetMapping
    public Result<List<Customer>> findAll(){
        //调用CustomerService实现查询所有Customer
        List<Customer> list = customerService.findAll();
        return new Result<List<Customer>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
